package com.cash.channel.svc.service.common;

import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.channel.ChannelModel;
import com.cash.channel.svc.dao.CredentialCheckFlowMapper;
import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.request.CredentialCheckRequest;
import com.cash.channel.api.model.response.CredentialCheckResponse;
import com.cash.channel.svc.logic.ChannelLogic;
import com.cash.channel.svc.model.ChannelInfo;
import com.cash.channel.svc.service.base.CredentialCheck;
import com.cash.user.model.UserInfoModel;
import com.cash.user.service.UserService;
import geex.core.service.model.user.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @blame taihao
 */
@Slf4j
@Service(Constant.CHANNEL.COMMON + Constant.SERVICE.撞库)
public class CommonCredentialCheck implements CredentialCheck {

    @Autowired
    private CredentialCheckFlowMapper credentialCheckFlowMapper;

    @Resource
    private UserService userService;

    @Resource
    private geex.core.service.UserService coreUserService;

    @Autowired
    private ChannelLogic channelLogic;

    @Override
    public ApiResponseModel<CredentialCheckResponse> doCredentialCheck(ApiRequestModel apiRequestModel) throws CashChannelException {
        CredentialCheckRequest credentialCheckRequest = JSON.parseObject(apiRequestModel.getData(), CredentialCheckRequest.class);
        ChannelInfo channelInfo = channelLogic.getChannelInfoByCode(apiRequestModel.getChannel());
        List<String> credentialConfigs = JSON.parseObject(channelInfo.getcCredentialConfig()).entrySet().stream().filter(entry->entry.getValue().equals(true))
                .map(Map.Entry::getKey).collect(Collectors.toList());
        CredentialCheckResponse credentialCheckResponse = checkCredential(credentialConfigs, credentialCheckRequest.getMd5());
        return new ApiResponseModel<>(true, 0, "", credentialCheckResponse);
    }

    private CredentialCheckResponse checkCredential(List<String> credentialConfigs, String md5) {
        UserInfoModel userInfoModel = userService.findUserByNameIDMD5(md5);
        if (userInfoModel == null) {
            return new CredentialCheckResponse(1, 0, null, null);
        }
        User coreUser = coreUserService.findUserByIdNo(userInfoModel.getcIdno());
        for (String config : credentialConfigs) {
            switch (config) {
                case "isNew":
                    return new CredentialCheckResponse(CAN_NOT_LOAN, IS_STOCK, REASON_STOCK, null);
                case "notOverdue":
                    if (coreUser.getXjdMaxOverdueDays()>0) {
                        return new CredentialCheckResponse(CAN_NOT_LOAN, IS_STOCK, REASON_OVERDUE, null);
                    }
                case "notTransit":
                    if (coreUser.getXjdRemainLoanCount()>0) {
                        return new CredentialCheckResponse(CAN_NOT_LOAN, IS_STOCK, REASON_TRANSIT, null);
                    }
                default:
                    break;
            }
        }
        return new CredentialCheckResponse(IS_CAN_LOAN, NOT_STOCK, null, null);
    }

}
